﻿using UnityEngine;
using System.Collections.Generic;
using TTGame;
/*
 * PuppetSkillCircle
 * 人物选择目标时，地上显示的技能圈
 *
 * 启用时传入SkillSelector，目标选择完成或者中断后将结果设置给SkillSelector。
 * PuppetSkillCircel的坐标和方向，由摇杆控制。
 *
 * 放在人物身上。
 *
 * 依赖：
 * SkillSelector
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/17/2017 5:53:16 PM
 */
namespace TTGameEngine {
public class PuppetSkillCircle : MonoBehaviour {

    const string SkillRange_PrefabPath = "Prefabs/Effects/CS2/TX_shifafanwei";
    const string SkillHit_PrefabPath = "Prefabs/Effects/CS2/TX_shifafanwei_04";
    const string SkillDir_PrefabPath = "Prefabs/Effects/CS2/TX_shifafanwei_03";
    const string SkillSector_PrefabPath = "Prefabs/Effects/CS2/TX_shifafanwei_02";
    const string SkillLock_PrefabPath = "Prefabs/Effects/CS2/TX_shifafanwei_02_1";

    Transform skillRange;
    Transform skillHit;
    SkillSelector m_selector;
    UISelectorInfo m_uiInfo;

    Vector3 m_selectedDir;
    Vector3 m_selectedAffectAreaPos;
    string m_selectedObjGID;
    Vector3 m_selectedObjPos;

    bool isSimulation = false;

    Vector3 SelectedDir {
        get { return m_selectedDir; }
        set { m_selectedDir = value; }
    }
    Vector3 SelectedAffectAreaPos {
        get { return m_selectedAffectAreaPos; }
        set { m_selectedAffectAreaPos = value; }
    }

    void Awake() {
        //m_puppet = GetComponent<puppet>;
    }

    // Update is called once per frame
    void Update() {
        updateSelector();

    }

    void updateSelector() {
        if(m_selector == null) {
            return;
        }
        //追随Puppet
        if(isSimulation == false) {
            skillRange.position = transform.position;
            skillHit.position = transform.position;

            SkillSelectorType selType = m_selector.SelectorType;
            switch(selType) {
                case SkillSelectorType.Sector: {
                        // 扇型
                        skillHit.forward = m_uiInfo.GetDir();
                    }
                    break;
                case SkillSelectorType.Direct: {
                        // 方向型
                        skillHit.forward = m_uiInfo.GetDir();
                    }
                    break;
                case SkillSelectorType.Area: {
                        // 范围型
                        skillHit.position = transform.position + m_uiInfo.GetPosOffset(m_selector.SelectorRadius);
                    }
                    break;
                case SkillSelectorType.Lock: {
                        // 选择型
                        skillHit.forward = m_uiInfo.GetDir();
                    }
                    break;
                case SkillSelectorType.SelfCenter: {
                        // 自身范围

                    }
                    break;
            }
        }

        if(m_uiInfo.SelectorState == SelectorStateID.SelectOK) {
            EndSelectMode();
        } else if(m_uiInfo.SelectorState == SelectorStateID.SelectCancel) {
            CancelSelectMode();
        }
    }

    public void StartSelectMode(SkillSelector selector, UISelectorInfo uiInfo) {
        enabled = true;
        m_selector = selector;
        m_uiInfo = uiInfo;
        SkillSelectorType selType = m_selector.SelectorType;
        isSimulation = m_uiInfo.IsSimulation;

        if(isSimulation == false) {
            GameObject rangePrefab = GameObjectLoader.Ins.GetPrefabObj(SkillRange_PrefabPath) as GameObject;
            GameObject rangeObj = ObjectRecycler.Instance.InstantiatePrefab(rangePrefab, transform.position, Quaternion.identity);
            skillRange = rangeObj.transform;

            GameObject hitCirclePrefab = null;
            switch(selType) {
                case SkillSelectorType.Sector: {
                        // 扇型
                        hitCirclePrefab = GameObjectLoader.Ins.GetPrefabObj(SkillSector_PrefabPath) as GameObject;
                    }
                    break;
                case SkillSelectorType.Direct: {
                        // 方向型
                        hitCirclePrefab = GameObjectLoader.Ins.GetPrefabObj(SkillDir_PrefabPath) as GameObject;
                    }
                    break;
                case SkillSelectorType.Area: {
                        // 范围型
                        hitCirclePrefab = GameObjectLoader.Ins.GetPrefabObj(SkillHit_PrefabPath) as GameObject;
                    }
                    break;
                case SkillSelectorType.Lock: {
                        // 选择型
                        hitCirclePrefab = GameObjectLoader.Ins.GetPrefabObj(SkillLock_PrefabPath) as GameObject;
                    }
                    break;
                case SkillSelectorType.SelfCenter: {
                        // 自身范围

                    }
                    break;
            }

            if(hitCirclePrefab != null) {
                GameObject hitCircleObj = ObjectRecycler.Instance.InstantiatePrefab(hitCirclePrefab, transform.position, Quaternion.identity);
                skillHit = hitCircleObj.transform;
            }
        }
    }

    public void EndSelectMode() {
        ClearCircle();
        if(m_selector != null) {
            SkillSelectorType selType = m_selector.SelectorType;
            switch(selType) {
                case SkillSelectorType.Sector: {
                        // 扇型
                        m_selector.SetResult_Sector(selType, m_selectedDir);
                    }
                    break;
                case SkillSelectorType.Direct: {
                        // 方向型
                        m_selector.SetResult_Direct(selType, m_selectedDir);
                    }
                    break;
                case SkillSelectorType.Area: {
                        // 范围型
                        m_selector.SetResult_Area(selType, m_selectedAffectAreaPos);
                    }
                    break;
                case SkillSelectorType.Lock: {
                        // 选择型
                        Puppet obj = m_uiInfo.GetSelectedObj();
                        if(obj == null) {
                            CancelSelectMode();
                        } else {
                            m_selector.SetResult_Lock(selType, obj.GetGID(), obj.Pos);
                        }
                    }
                    break;
                case SkillSelectorType.SelfCenter: {
                        // 自身范围
                        m_selector.SetResult_SelfCenter(selType);
                    }
                    break;

            }

            m_selector = null;
        }
    }

    public void CancelSelectMode() {
        ClearCircle();
        if(m_selector != null) {
            m_selector.Cancel();
            m_selector = null;
        }

    }

    public bool IsOnUse() {
        return m_selector != null;
    }

    void ClearCircle() {
        enabled = false;
        if(skillRange != null) ObjectRecycler.Instance.Deactivate(skillRange.gameObject);
        if(skillHit != null) ObjectRecycler.Instance.Deactivate(skillHit.gameObject);
        skillRange = null;
        skillHit = null;
    }
}
}